﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace CalendarApplication
{
    public partial class frmLogin : Form
    {
        private CalendarServiceRef.User user;
        public bool good = false;

        public frmLogin()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            good = false;
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            lblLoginError.Visible = false;
            CalendarServiceRef.CalendarServiceClient sr = new CalendarServiceRef.CalendarServiceClient();
            try
            {
                string un = txtUsername.Text.ToString();
                string pw = txtPassword.Text.ToString();

                user = sr.Login(un, pw);
                if (user != null)
                {
                    Form1 frmForm1 = new Form1(this, user);
                    this.Hide();
                    frmForm1.Show();
                    good = true;
                    //this.Close();
                }
                else
                {
                    lblLoginError.Visible = true;
                    txtPassword.Clear();
                    txtUsername.Focus();
                    txtUsername.SelectAll();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lklbRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmRegister frmReg = new frmRegister();
            frmReg.ShowDialog();
        }
    }
}